package com.droidathletics.control;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.database.Cursor;

import com.droidathletics.boundary.MainMenu;
import com.droidathletics.boundary.dao.MicrocycleDAOAdapter;
import com.droidathletics.entity.Competition;
import com.droidathletics.entity.Date;
import com.droidathletics.entity.Microcycle;
import com.droidathletics.entity.TypeTraining;

public class ManageMicrocycle {
	public ManageMicrocycle(Context context)
	{
		microcycleDAO = new MicrocycleDAOAdapter(context);
	}
	
	public MicrocycleDAOAdapter getMicrocycleDAO() {
		return microcycleDAO;
	}

	public void setMicrocycleDAO(MicrocycleDAOAdapter microcycleDAO) {
		this.microcycleDAO = microcycleDAO;
	}

	private MicrocycleDAOAdapter microcycleDAO;

	public String createMicrocycle(String macrocycleName,
			String numberOfWeek, String period, String stage, String microCycleType,
			String objective, String numberOfHoursPerWeek,
			ArrayList<TypeTraining> trainings)
	{
		String validateInformation = checkBusinessRules(period, stage, microCycleType, objective, trainings);
		
		if ( validateInformation.equals("")) 
		{
		  Microcycle newMicrocycle = new Microcycle(macrocycleName, Integer.parseInt(numberOfWeek), period, stage,
				  microCycleType, objective, Integer.parseInt(numberOfHoursPerWeek));
		
		  microcycleDAO.createMicrocycle(macrocycleName, numberOfWeek, period, stage, microCycleType, objective,
					numberOfHoursPerWeek, trainings);
		
		}
		
		return validateInformation;
	}
		
	public String updateMicrocycle(Microcycle oldMicrocycle, String numberOfWeek, String period, String stage, 
			String microCycleType, String objective, String numberOfHoursPerWeek,
			ArrayList<TypeTraining> trainings)
	{
		Microcycle updMicrocycle = oldMicrocycle;
		String validateInformation = checkBusinessRules(period, stage, microCycleType, objective, trainings);
		
		if ( validateInformation.equals("")) 
		{
		    microcycleDAO.updateMicrocycle(updMicrocycle.getNumberOfWeek(), updMicrocycle.getMacrocycleName(),
			numberOfWeek, period, stage, microCycleType, objective,
			numberOfHoursPerWeek, trainings);
		    
		    return "";
		}
		
		return validateInformation;
	}
	
    public ArrayList<Microcycle> readAllMicrocycles(String macrocycleName)
    {
        Cursor cursor = microcycleDAO.readMicrocycle(macrocycleName);
        ArrayList<Microcycle> listDBMicrocycles = new ArrayList<Microcycle>();
        MainMenu.drawablesMicrocycles.clear();
		if (cursor.moveToFirst()){
			do
			{
			   int numberOfWeek = cursor.getInt(cursor.getColumnIndex("numberofweek"));
               String period = cursor.getString(cursor.getColumnIndex("period"));
               String stage = cursor.getString(cursor.getColumnIndex("stage"));
               String microCycleType = cursor.getString(cursor.getColumnIndex("microcycletype"));
               String objective = cursor.getString(cursor.getColumnIndex("objective"));
               int numberOfHoursPerWeek = cursor.getInt(cursor.getColumnIndex("numberhoursperweek"));
               
               ArrayList<TypeTraining> trainings = new ArrayList<TypeTraining>();
               trainings = Microcycle.initializeTrainings(trainings);
     
               
               int indexTraining = 0;
               String[] trainingValues = new String[] { "analacvelocidad",
               		"anlacresistveloc", "anaaerobiomixto", "aerobico", "fuerzamaxima", "fuerzarapida", 
               		"fuerzaresistencia", "flexibilidad", "capcoordinativas", "tecnica",
               		"tecnicatactica", "psicologica", "pbiologica", "pteorica", "competencias", "controles"};
               
               for ( TypeTraining t : trainings)
               {
               	 trainings.get(indexTraining).setVolume( 
               			          cursor.getInt(cursor.getColumnIndex(trainingValues[indexTraining])) );
               	 indexTraining++;
               }
               MainMenu.addDrawableMicrocycles();
               listDBMicrocycles.add( new Microcycle(macrocycleName, numberOfWeek, period, stage,
           			microCycleType, objective, numberOfHoursPerWeek, trainings) );
               
			} while ( cursor.moveToNext());

       }
       cursor.close();
       
       return listDBMicrocycles;
    }
    
    public boolean deleteMicrocycle(int numberOfWeek, String macrocycleName)
    {
    	microcycleDAO.deleteMicrocycle(numberOfWeek, macrocycleName);
    	return true;
    }
    
	public String checkBusinessRules(String period, String stage, String microCycleType, 
			 String objective, ArrayList<TypeTraining> trainings)
	{
		
		if ( !checkNamesAreValid(period) )
		{
			return ("The period field must be contain only characters.");
		}
		
		if ( !checkNamesAreValid(stage) )
		{
			return ("The stage field must be contain only characters.");
		}
		
		if ( !checkNamesAreValid(microCycleType) )
		{
			return ("The microcycle type field must be contain only characters.");
		}
		
		if ( !checkNamesAreValid(objective) )
		{
			return ("The objective field must be contain only characters.");
		}
		
		return ("");
	}
	
	private boolean checkNamesAreValid(String name)
	{
        Pattern patternName = Pattern.compile("[^A-Za-z������ ]");
        Matcher matcherName = patternName.matcher(name);

        boolean isString = false;
        while (matcherName.find())
        {
                isString = true;
        }
        return !isString;
	}
}
